package com.zzy.demo.methods;

import java.util.List;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;


/**
 * 事务和管道都是异步模式。在事务和管道中不能同步查询结果。比如下面两个调用，都是不允许的：
 * @author guokaige
 *
 */
public class RedisPipel {

    public static void main(String[] args) {
        common();
        pipeline();
    }

    public static void pipeline() {
        Jedis jedis = new Jedis("localhost",6379);
     //   jedis.auth("123456");
        Pipeline pipeline = jedis.pipelined();
        long start = System.currentTimeMillis();
        for(int i=0; i<1000; i++){
            pipeline.set("p" + i, "value" + i);
        }
        List<Object> results = pipeline.syncAndReturnAll();
        long end = System.currentTimeMillis();
        System.out.println("Pipelined cost time : " +((end-start)/1000.0) + " seconds");
        jedis.disconnect();
    }

    public static void common(){
        Jedis jedis = new Jedis("localhost",6379);
     //   jedis.auth("123456");
        long start = System.currentTimeMillis();
        for(int i=0; i<1000; i++){
            jedis.set("p" + i, "value" + i);
        }
        long end = System.currentTimeMillis();
        System.out.println("common cost time : " +((end-start)/1000.0) + " seconds");
        jedis.disconnect();
    }

}